package com.kai.service.wechat.handle;

import com.kai.service.data.dto.wechat.receive.MessageReceiveDto;
import com.kai.service.data.dto.wechat.reply.MessageReplyDto;
import com.kai.service.util.XmlUtil;

import java.util.Date;

public abstract class MessageHandle<T extends MessageReceiveDto> {

    protected abstract Object messageAnalysis(T message);

    @SuppressWarnings("unchecked")
    public Object messageAnalysisEntrance(MessageReceiveDto messageReceiveDto) {
        return messageAnalysis((T) messageReceiveDto);
    }

    /**
     * 消息回复
     *
     * @param messageDto
     * @param messageReplyDto
     * @return
     * @param <M>
     */
    protected <M extends MessageReplyDto> Object messageReply(MessageReceiveDto messageDto, M messageReplyDto) {
        messageReplyDto.setToUserName(messageDto.getFromUserName());
        messageReplyDto.setFromUserName(messageDto.getToUserName());
        messageReplyDto.setCreateTime(new Date().getTime());

        return XmlUtil.objectStrToXml(messageReplyDto);
    }
}
